const sqlHelper = require('../persist/sqlHelper');
const logger = require('../logs/logger');

module.exports = {
    add(s) {
        var sql = `INSERT INTO BRAND (BIZ, NAME, NICK, DIGEST, LOGOURL, UPDATEAT) 
        SELECT ?,?,?,?,?,? FROM DUAL 
        WHERE NOT EXISTS (SELECT 1 FROM BRAND WHERE BIZ = ?)`;
        var params = [s.biz, s.name, s.nick, s.digest, s.logoUrl, s.updateAt, 
            s.biz];

        sqlHelper.execute(conn => {
            conn.query(sql, params, (err, resp) => {
                if (err) {
                    logger.exception(err);
                    return;
                }
                logger.debug(`INSERT BRAND: ${resp}`);
            });
        });
    },

    single(id, callback) {
        var sql = 'SELECT * FROM BRAND WHERE BIZ = ? LIMIT 1;';
        var params = [ id ];

        sqlHelper.execute(conn => {
            conn.query(sql, params, (err, resp) => {
                if (err) {
                    logger.exception(err);
                    return;
                }
                logger.debug(`SELECT BRAND: ${resp}`);
                
                if (callback && resp.length === 1) {
                    callback(resp[0]);
                }
            });
        });
    }
};